﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Obra.SmugMug.Api
{
    /// <summary>
    /// Models the keywords for an album, or an image.
    /// 
    /// TODO: this class should really implement ICollection (IHashSet?)
    /// </summary>
    public class Keywords 
    {
        private readonly HashSet<string> keywords =
            new HashSet<string>();

        /// <summary>
        /// The delimiter used by smugmug.com for keyword separation.
        /// </summary>
        public const string Delimiter = ";";

        /// <summary>
        /// Returns true if the keyword specified is one of the keywords in the
        /// collection.
        /// </summary>
        /// <param name="keyword">
        /// The keyword to search for in the collection.
        /// </param>
        public bool Contains(string keyword)
        {
            return this.keywords.Contains(keyword);
        }

        /// <summary>
        /// Add a keyword to the collection.
        /// </summary>
        /// <param name="keyword">
        /// The keyword to add.
        /// </param>
        public void Add(string keyword)
        {
            this.keywords.Add(keyword);
        }

        /// <summary>
        /// Remove a keyword from the collectin.
        /// </summary>
        /// <param name="keyword">
        /// The keyword to remove.
        /// </param>
        public void Remove(string keyword)
        {
            this.keywords.Remove(keyword);
        }

        /// <summary>
        /// The number of keywords in the collection.
        /// </summary>
        public int Count
        {
            get { return this.keywords.Count; }
        }

        /// <summary>
        /// Construct an empty keyword collection.
        /// </summary>
        public Keywords()
        {
            // Do nothing.
        }

        /// <summary>
        /// Parse the string returned by smugmug.com, and the keywords to the
        /// collection.
        /// 
        /// An example keywords string is
        ///   animal; dog; black labrador; tonue;
        /// </summary>
        /// <param name="keywords">
        /// The keywords string to parse.
        /// </param>
        public Keywords(string keywords)
        {
            foreach (string keyword in keywords.Split(
                new string[] { Keywords.Delimiter },
                StringSplitOptions.RemoveEmptyEntries))
            {
                this.keywords.Add(keyword.Trim());
            }
        }

        /// <summary>
        /// Add the list of keywords to the collection.
        /// </summary>
        /// <param name="keywords"></param>
        public Keywords(IEnumerable<string> keywords)
        {
            foreach (var keyword in keywords)
            {
                this.keywords.Add(keyword);
            }
        }
    }
}
